package com.example.bookstore;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private MyDataBaseHelper helper;
    private SQLiteDatabase db;
    private static final String TABLE_NAME = "Book";
    private static final String TABLE_NAME2 = "Category";
    private List<Book> BookList=new ArrayList<>();

    @SuppressLint("Range")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initBook();
        BookAdapter adapter =new BookAdapter(MainActivity.this,R.layout.listdetail,BookList);
        ListView listView=(ListView) findViewById(R.id.list_item);
        listView.setAdapter(adapter);

        Button addData = (Button) findViewById(R.id.insert);
        addData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,AddBookActivity.class);
                startActivity(intent);
            }
        });
        Button addCategory = (Button) findViewById(R.id.insert_c);
        addCategory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,AddCategoryActivity.class);
                startActivity(intent);
            }
        });
    }

    public void initBook(){
        //实例化SQLiteOpenHelper的子类对象-MyDataBaseHelper
        helper = new MyDataBaseHelper(this, "dbName", null, 1);//dbName数据库名
        db = helper.getWritableDatabase();//获取到了 SQLiteDatabase 对象
        Cursor cursor = db.rawQuery("select b.name,c.Category_name,b.price from Book b left outer join Category c on b.category_id=c.id;",null);
        if (cursor.moveToFirst()){//遍历对象
            do {
                //向适配器中添加数据
                 @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name"));

                 @SuppressLint("Range") String category_name = cursor.getString(cursor.getColumnIndex("category_name"));

                 @SuppressLint("Range") String price = cursor.getString(cursor.getColumnIndex("price"));

                Book book = new Book(""+name,""+category_name,""+price);
                BookList.add(book);
            }while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        }
}

